part of '../component.dart';

enum TextFieldStyle {
  outLine,
  underLine,
  none,
}

class UITextField extends StatefulWidget {
  const UITextField({
    super.key,
    this.data,
    this.valueChanged,
    this.style = TextFieldStyle.none,
    this.prefix,
    this.prefixText,
    this.suffix,
    this.suffixText,
    this.prefixWidth,
    this.suffixWidth,
    this.height,
    this.title,
    this.margin = EdgeInsets.zero,
    this.padding,
    this.enabled = true,
    this.borderRaius,
    this.isClear = false,
    this.obscureText = false,
    this.keyboardType,
    this.onChanged,
    this.titleStyle,
    this.contentStyle,
    this.hintStyle,
    String? hintText,
    this.fontSize,
    this.prefixIcon,
    this.suffixIcon,
    this.onPrefixTap,
    this.onSuffixTap,
    this.backgroundColor,
    this.maxLength,
    this.maxLengthWithoutCounter,
    this.minLines,
    this.maxLines = 1,
    this.textAlign = TextAlign.start,
    this.counter,
    this.inputFormatters,
    this.focusNode,
    this.onSubmitted,
    this.autoFocus = false,
  }) : hintText = hintText ?? '请输入';
  final String? data;
  final ValueChanged<String>? valueChanged;
  final TextFieldStyle style;
  final String? prefixText;
  final String? suffixText;
  final Widget? prefix;
  final Widget? suffix;
  final double? prefixWidth;
  final double? suffixWidth;
  final double? height;
  final String? title;
  final EdgeInsets margin;
  final bool enabled;
  final BorderRadius? borderRaius;
  final bool isClear;
  final bool obscureText;
  final String? hintText;
  final TextInputType? keyboardType;
  final ValueChanged<String>? onChanged;
  final TextStyle? titleStyle;
  final TextStyle? contentStyle;
  final TextStyle? hintStyle;
  final double? fontSize;
  final IconData? prefixIcon;
  final IconData? suffixIcon;
  final VoidCallback? onPrefixTap;
  final ValueChanged<String>? onSuffixTap;
  final Color? backgroundColor;
  final int? minLines;
  final int? maxLines;
  final int? maxLength;
  final TextAlign textAlign;
  final Widget? counter;
  final EdgeInsets? padding;
  final int? maxLengthWithoutCounter;
  final List<TextInputFormatter>? inputFormatters;
  final FocusNode? focusNode;
  final ValueChanged<String>? onSubmitted;
  final bool autoFocus;
  @override
  State<UITextField> createState() => _UITextFieldState();
}

class _UITextFieldState extends State<UITextField>
    with AutomaticKeepAliveClientMixin {
  final controller = TextEditingController();
  Widget? prefix;
  Widget? suffix;
  TextStyle? titleStyle;
  TextStyle? contentStyle;
  TextStyle? hintStyle;
  EdgeInsets? contentPadding;
  double textFieldHeight = 44.r;

  void config() {
    if (widget.height != null) {
      textFieldHeight = widget.height!;
    }
    titleStyle = widget.titleStyle ?? ITheme.text.title16;
    contentStyle = widget.contentStyle ??
        (widget.fontSize != null
            ? TextStyle(color: ITheme.titleColor, fontSize: widget.fontSize)
            : ITheme.text.titleMedium);

    hintStyle = widget.hintStyle ??
        (widget.fontSize != null
            ? TextStyle(color: ITheme.hintColor, fontSize: widget.fontSize)
            : ITheme.text.hintStyle);
    controller.text = widget.data ?? '';
    suffix = widget.suffix;
    if (suffix == null) {
      if (widget.suffixText != null) {
        suffix = InkWell(
          onTap: () {
            if (widget.onSuffixTap != null) {
              widget.onSuffixTap!(controller.text);
            }
          },
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                width: widget.suffixWidth,
                padding: widget.suffixWidth == null
                    ? EdgeInsets.symmetric(horizontal: 10.r)
                    : EdgeInsets.zero,
                child: Text(
                  widget.suffixText!,
                  style: ITheme.text.content15,
                ),
              ),
            ],
          ),
        );
      }
      IconData? suffixIcon = widget.suffixIcon;
      if (widget.isClear) {
        suffixIcon = Icons.clear;
      }
      if (suffixIcon != null) {
        suffix = InkWell(
          onTap: widget.isClear
              ? controller.clear
              : () {
                  if (widget.onSuffixTap != null) {
                    widget.onSuffixTap!(controller.text);
                  }
                },
          child: Container(
            height: textFieldHeight,
            width: textFieldHeight,
            alignment: Alignment.center,
            child: Icon(
              suffixIcon,
              color: Colors.black,
              size: 22.r,
            ),
          ),
        );
      }
    }

    prefix = widget.prefix;
    if (prefix == null) {
      if (widget.prefixText != null) {
        prefix = InkWell(
          onTap: widget.onPrefixTap,
          child: Container(
            height: textFieldHeight,
            padding: widget.prefixWidth == null
                ? EdgeInsets.symmetric(horizontal: 10.r)
                : EdgeInsets.zero,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  widget.prefixText!,
                  style: ITheme.text.titleMedium,
                ),
              ],
            ),
          ),
        );
      }
      if (widget.prefixIcon != null) {
        prefix = InkWell(
          onTap: widget.onPrefixTap,
          child: Container(
            height: textFieldHeight,
            width: textFieldHeight,
            alignment: Alignment.center,
            child: Icon(
              widget.prefixIcon,
              color: Colors.black,
              size: 22.r,
            ),
          ),
        );
      }
    }

    contentPadding = widget.padding;
    if (contentPadding == null) {
      double edgeLeft = 0;
      if (widget.prefix == null &&
          (widget.style == TextFieldStyle.outLine ||
              widget.borderRaius != null)) {
        edgeLeft = 10;
      }
      double edgeRight = 0;
      if (widget.suffix == null &&
          (widget.style == TextFieldStyle.outLine ||
              widget.borderRaius != null)) {
        edgeRight = 10;
      }
      contentPadding = EdgeInsets.fromLTRB(edgeLeft, 0, edgeRight, 0);
      if (widget.maxLines != 1 && widget.maxLines != null) {
        contentPadding = const EdgeInsets.all(10);
      }
    }
  }

  @override
  void initState() {
    config();

    super.initState();
  }

  @override
  void didUpdateWidget(covariant UITextField oldWidget) {
    controller.text = widget.data ?? '';
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  TextField _textField() {
    return TextField(
      controller: controller,
      focusNode: widget.focusNode,
      onChanged: (val) {
        if (widget.maxLengthWithoutCounter != null) {
          if (val.length > widget.maxLengthWithoutCounter!) {
            val = val.substring(0, 30);

            controller.text = val;
          }
        }
        if (widget.onChanged != null) {
          widget.onChanged!(val);
        }
      },
      autofocus: widget.autoFocus,
      keyboardType: widget.keyboardType,
      obscureText: widget.obscureText,
      scrollPadding: EdgeInsets.zero,
      selectionControls: CupertinoTextSelectionControls(),
      cursorColor: ITheme.focusColor,
      style: contentStyle,
      maxLength: widget.maxLength,
      minLines: widget.minLines,
      maxLines: widget.maxLines,
      textAlign: widget.textAlign,
      textAlignVertical: TextAlignVertical.center,
      inputFormatters: widget.inputFormatters,
      onSubmitted: widget.onSubmitted,
      decoration: InputDecoration(
        enabled: widget.enabled,
        contentPadding: contentPadding,
        hintText: widget.hintText,
        hintStyle: hintStyle,
        prefixIconConstraints: BoxConstraints(minWidth: 0, maxWidth: 120.r),
        prefixIcon: prefix ??
            SizedBox(
              height: textFieldHeight,
              width: contentPadding!.left,
            ),
        suffixIcon: suffix ??
            SizedBox(
              height: textFieldHeight,
              width: contentPadding!.right,
            ),
        suffixIconConstraints: BoxConstraints(minWidth: 0, maxWidth: 120.r),
        counter: widget.counter,
        counterStyle: ITheme.text.content10,
        enabledBorder: _inputBorder(ITheme.borderColor),
        focusedBorder: _inputBorder(ITheme.focusColor),
        errorBorder: _inputBorder(ITheme.errorColor),
        disabledBorder: _inputBorder(ITheme.disableColor),
        fillColor: widget.backgroundColor,
        filled: widget.backgroundColor != null,
        isCollapsed: widget.maxLines == 1,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return Container(
      margin: widget.margin,
      height: widget.maxLines != 1
          ? null
          : widget.title == null
              ? textFieldHeight
              : null,
      child: widget.title == null
          ? _textField()
          : Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  padding: EdgeInsets.only(top: 10.r),
                  alignment: Alignment.centerLeft,
                  child: Text(
                    widget.title!,
                    style: titleStyle,
                  ),
                ),
                SizedBox(
                    height: widget.maxLines != 1 ? null : textFieldHeight,
                    child: _textField()),
              ],
            ),
    );
  }

  InputBorder _inputBorder(Color color) {
    switch (widget.style) {
      case TextFieldStyle.outLine:
        return OutlineInputBorder(
            borderSide: BorderSide(color: color, width: 0.5),
            borderRadius: widget.borderRaius ?? BorderRadius.zero);
      case TextFieldStyle.underLine:
        return UnderlineInputBorder(
            borderSide: BorderSide(color: color, width: 0.5));
      case TextFieldStyle.none:
        return InputBorder.none;
    }
  }

  @override
  bool get wantKeepAlive => true;
}

class ReasonTextField extends StatefulWidget {
  const ReasonTextField({
    Key? key,
    this.title,
    this.maxLines = 3,
    this.maxLength = 100,
    this.value,
    this.onChanged,
    this.hintText,
    this.padding,
    this.margin,
    this.isNeed = false,
  }) : super(key: key);
  final String? title;
  final int maxLines;
  final int maxLength;
  final String? value;
  final ValueChanged? onChanged;
  final String? hintText;
  final EdgeInsets? padding;
  final EdgeInsets? margin;
  final bool isNeed;
  @override
  State<ReasonTextField> createState() => _ReasonTextFieldState();
}

class _ReasonTextFieldState extends State<ReasonTextField>
    with AutomaticKeepAliveClientMixin {
  final textCtrl = TextEditingController();

  @override
  void initState() {
    super.initState();
    textCtrl.text = widget.value ?? '';
  }

  @override
  void didUpdateWidget(covariant ReasonTextField oldWidget) {
    super.didUpdateWidget(oldWidget);
    textCtrl.text = widget.value ?? '';
  }

  @override
  void dispose() {
    super.dispose();
    textCtrl.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      padding: widget.padding,
      margin: widget.margin,
      decoration: const BoxDecoration(
        border: Border(
          bottom: BorderSide(color: Color(0xfff0f2f5)),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (widget.title != null)
            Text.rich(TextSpan(
                text: widget.isNeed ? '*' : '',
                children: [
                  TextSpan(
                      text: widget.title,
                      style: TextStyle(
                          color: const Color(0xff333333), fontSize: 17.sp)),
                ],
                style: TextStyle(color: Colors.red, fontSize: 18.sp))),
          TextField(
            controller: textCtrl,
            onChanged: widget.onChanged,
            maxLength: widget.maxLength,
            maxLines: widget.maxLines,
            style: TextStyle(color: const Color(0xff333333), fontSize: 15.sp),
            enabled: widget.onChanged != null,
            selectionControls: CupertinoTextSelectionControls(),
            decoration: InputDecoration(
              enabledBorder: InputBorder.none,
              focusedBorder: InputBorder.none,
              counterStyle: ITheme.text.content10,
              hintText: widget.hintText,
              hintStyle:
                  TextStyle(color: const Color(0xffbbbbbb), fontSize: 15.sp),
            ),
          ),
        ],
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
